* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"


* -------------------------------------------------------------------------------------------------
* DI medical spending by entry year---alternative specifications
* -------------------------------------------------------------------------------------------------

* Which controls included in each regression
* Set B - spending controls (Fig 2b, eqn for spending at 51 and 52)
* + Spec 1:   years enrolled
* + Spec 2:   years enrolled by county (baseline)
* + Spec 3-4: years enrolled by county, sex by age (demog diffs in who enrolls when)
* + Spec 5:   years enrolled by county, rfrnc_yr by county
* + Spec 6:   years enrolled by entry month
* + Spec 7:   years enrolled by county, years enrolled by entry month

local controls_01 i.years_since_covstart
local controls_02 i.years_since_covstart##i.fipscounty_firstnm_g
local controls_03 i.years_since_covstart##i.fipscounty_firstnm_g i.male_init##i.age_year_covstart_fill
local controls_04 i.years_since_covstart##i.fipscounty_firstnm_g i.male_init##i.age
local controls_05 i.years_since_covstart##i.fipscounty_firstnm_g i.rfrnc_yr##i.fipscounty_firstnm_g
local controls_06 i.years_since_covstart##i.covstart_month
local controls_07 i.years_since_covstart##i.fipscounty_firstnm_g i.years_since_covstart##i.covstart_month

local sample_main 1
local sample main
local y tot_pmt
local clust county_mofd
local x i.covstart_year

* Which controls to include in each regression
local controls_01 i.years_since_covstart
local controls_02 i.years_since_covstart##i.fipscounty_firstnm_g

* DI entrant cohort average Medicare spending
local clust county_mofd
local y tot_pmt 
local x covstart_year

* Control variables
qui foreach ctrl in 01 02 03 04 05 {
  noisily di _newline "Control spec: `ctrl'"
  noisily di "Fixed effects: `controls_`ctrl''"
  
  * Load estimates
  local results_stub $SSDIMed/results/estimates/x-i.`x'/x-i.`x'_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'
  estimates use "`results_stub'.ster"
  
  * Calculate y effect, CI for each level of x variable (_cons plus x-var effect)
  local addvar 0, 0, 0
  local names : colfullnames e(b)
  foreach name of local names {
    if "`name'" == "_cons" continue
    lincom _cons + `name'
    local addvar `addvar', `"`name' + _cons"', `r(estimate)', `r(se)'
  }
  regsave, addvar(`addvar') detail(all)
  
  * which variables to keep
  drop if var == "0"
  keep if strpos(var, "+ _cons") > 0
  
  * confidence intervals for added variables
  gen double t_ci = abs(invttail(`e(df_r)', (1 - 0.95) / 2))
  qui gen double ci_lower = coef - t_ci*stderr
  qui gen double ci_upper = coef + t_ci*stderr
  rename (coef ci_lower ci_upper) (`y' `y'_ci_lo `y'_ci_hi)
  keep var `y' `y'_ci_lo `y'_ci_hi clustvar
  
  * numeric level for the x-variable (a factor)
  gen `x' = substr(var, 1, strpos(var, "."))
  replace `x' = regexr(`x', "(b\.)|(\.)", "")
  destring `x', replace
  order `x', after(var)
  
  * Units
  * tot_pmt in $1,000
  * mortality in deaths per 10,000
  foreach var in `y' `y'_ci_lo `y'_ci_hi {
    if "`y'" == "tot_pmt"        replace `var' = `var'/1000
    if strpos("`y'", "died") > 0 replace `var' = `var'*10000
  }
  
  * Save
  tempfile `x'_`y'
  save ``x'_`y'', replace
  noisily list, sep(0)
}




** EOF
